$(function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    // 获取代言列表
    var result = Model.represent.representList();
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    for (var i = 0; i < result.data.length; i++) {
        var item = '<div class="weui-cells"><div class="weui-cell"><div class="shop-vendor-left-mine"><div class="portrait-block"><img class="represent-img" src="' + result.data[i].photo + '"></div></div><div class="shop-vendor-right m-shop-vendor-info"><label class="vendor-level">v' + result.data[i].grade + '</label><h3 class="text-1-6 m-represent-item">';
        result.data[i].nickanme.length >= 7 ? item += result.data[i].nickanme.substr(0, 7) + '...' : item += result.data[i].nickanme 
        item += '</h3><div class="vendor-info text-left"><div class="weui-cell"><div class="weui-cell__hd"><span>代言费：</span></div><div class="weui-cell__bd"><span>￥</span><span>' + result.data[i].price + '</span></div><a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default sure-daiyan" data-id="' + result.data[i].businessId + '">确认代言</a></div></div></div></div></div>';
        $('.represent-list').append(item);
    }
    loading.hide();
});

$(document).on('click', '.sure-daiyan', function () {
    var id = $(this).attr('data-id');
    weui.confirm('确定要代言吗？', {
        title: '系统提示',
        buttons: [{
            label: '取消',
            type: 'default',
            onClick: function () {
                console.log('no')
            }
        }, {
            label: '代言',
            type: 'primary',
            onClick: function () {
                var result = Model.represent.chooseRepresent({
                    businessId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.reload();
                    return false;
                }
                alert(result.data);
                location.href = 'represent.html';
            }
        }]
    });
});